package com.service.dialtest.controller;/**
 * @author ：hjl
 * @date ：Created in 2021/10/28 19:20
 * @description：
 */

import com.service.dialtest.dto.CheckTenantDto;
import com.service.dialtest.dto.CloudUserDto;
import com.service.dialtest.dto.TenantDto;
import com.service.dialtest.entity.CloudUserInfo;
import com.service.dialtest.service.CloudUserService;
import com.service.config.utils.ModelMapperUtil;
import com.service.config.utils.RedisUtil;
import com.service.config.utils.Result;
import com.service.config.utils.UserTokenManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

import static com.service.config.constant.Constant.*;

/**
 * @Description:
 * @Author: hjl
 * @Date: 2021/10/28 19:20
 */
@RestController
@RequestMapping("/cloudUser")
@Api(value = "云管用户信息管理",description = "云管用户信息管理")
public class CloudUserController {

    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private CloudUserService cloudUserService;

    @PostMapping("checkApiAccount")
    @ApiOperation(value = "校验云管平台的账号和密码")
    @ResponseBody
    public Result checkTenant(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken,
                              @RequestBody CheckTenantDto checkTenantDto) {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                Map check = cloudUserService.check(checkTenantDto, userId);
                if (check.containsKey("errorMsg")) {
                    return Result.ok().setData(check);
                }
                return Result.ok();
            }else {
                return Result.failure(USERNAME_Off_SITE, usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }

    @PostMapping("addApiAccount")
    @ApiOperation(value = "增加云管api账号和密码")
    @ResponseBody
    public Result addApiAccount(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken,
                                @RequestBody TenantDto tenantDto) throws Exception {

        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                List<CloudUserInfo> screenUserList = cloudUserService.insertOrUpdate(ModelMapperUtil.strictMap(tenantDto, CloudUserInfo.class), userId);
                if (screenUserList != null) {
                    return Result.failure(CLOUD_CAMPUS_ACCOUNT_SAME, "云管理账号同域名已存在，是否覆盖");
                }
                return Result.ok();
            }else {
                return Result.failure(USERNAME_Off_SITE, usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }
    @PostMapping("updateApiAccount")
    @ApiOperation(value = "更新云管的密码")
    @ResponseBody
    public Result updateApiAccount(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken,
                                   @RequestBody TenantDto tenantDto) {

        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                cloudUserService.updateApiAccount(ModelMapperUtil.strictMap(tenantDto, CloudUserInfo.class));
                return Result.ok();
            }else {
                return Result.failure(USERNAME_Off_SITE, usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }
    @GetMapping("deleteApiAccount")
    @ApiOperation(value = "删除云管理api账号")
    @ResponseBody
    public Result deleteApiAccount(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken,
                                   @RequestParam("screen_id") @ApiParam("api账号id") String screen_id) {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                cloudUserService.deleteById(screen_id);
                return Result.ok();
            }else {
                return Result.failure(USERNAME_Off_SITE, usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }
    @GetMapping("enableAndDisable")
    @ApiOperation(value = "api账号的启用和禁用")
    @ResponseBody
    public Result enableAndDisable(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken,
                                   @RequestParam("cloud_user_id") @ApiParam("云管用户id") String cloud_user_id,
                                   @ApiParam("状态：true-启用，false-禁用")@RequestParam("status") Boolean status,
                                   @ApiParam("域名")@RequestParam("domain") String domain) {

        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                cloudUserService.serviceStatusBind(cloud_user_id,status,userId,domain);
                return Result.ok();
            }else {
                return Result.failure(USERNAME_Off_SITE, usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }
    @GetMapping("findAllApi")
    @ApiOperation(value = "云管账号列表")
    @ResponseBody
    public Result findAllApi(@RequestHeader(value = "cloud-Auth-Token") @ApiParam("token") String cloudAuthToken) {
        if (cloudAuthToken != null && !cloudAuthToken.equals("")) {
            String userId = UserTokenManager.getUserId(cloudAuthToken);
            if (redisUtil.exist(cloudAuthToken + userId)) {
                List<CloudUserDto> screenUserList = cloudUserService.queryAll(userId);
                return Result.ok().setData(screenUserList);
            }else {
                return Result.failure(USERNAME_Off_SITE, usernameOffSite);
            }
        }
        return Result.failure(USER_NOT_LOGIN, "用户未登录");
    }
}
